package fragmentson;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import java.util.ArrayList;

import activity.SonActivity;
import bean.EntityForSimple;
import bean.MessageForSimpleList;
import fragmentson.backhandle.BackHandledFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import service.ApiUtil;
import util.adapter.DevicesAdapter;
import util.myview.springview.DefaultHeader;
import util.myview.springview.SpringView;

/**
 * 登录
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class DeviceList extends BackHandledFragment {

	private View rootView;
	private ListView lv_devices;
	private Intent intent;
	private Bundle bundle;
	private DevicesAdapter adapter;

	private SpringView springView;
	private ArrayList<EntityForSimple> mDatas = new ArrayList<>();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_devicelist, null);
		initView();
		initData();
		return rootView;
	}

	public void initView(){
		((TextView) rootView.findViewById(R.id.tv_title)).setText(R.string.title_mycar);
		rootView.findViewById(R.id.iv_add).setOnClickListener(listener);
		lv_devices = ((ListView) rootView.findViewById(R.id.lv_devices));
		lv_devices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				for(EntityForSimple mData:DevicesAdapter.mDatas){
					mData.setIschecked(false);
				}
				DevicesAdapter.mDatas.get(position).setIschecked(true);
				adapter.notifyDataSetChanged();
			}
		});
		initSpringView();
	}

	public void initData(){
		ApiUtil.getApiService().getDevices(DemoApplication.getToken()).enqueue(new Callback<MessageForSimpleList>() {
			@Override
			public void onResponse(Call<MessageForSimpleList> call, Response<MessageForSimpleList> response) {
				try {
					springView.onFinishFreshAndLoad();
					MessageForSimpleList entity = response.body();
					if(entity.getCode().equals("10008")){
						mDatas = entity.getData();
						adapter = new DevicesAdapter(getActivity(),mDatas);
						adapter.setCallListener(new DevicesAdapter.callback() {
							@Override
							public void listener(int item) {
								((SonActivity) getActivity()).addFragment(DeviceParticulars.newInstance());
							}
						});
						lv_devices.setAdapter(adapter);
					}else{
						Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(Call<MessageForSimpleList> call, Throwable t) {
				Log.d("exception",t.getMessage());
				Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
			}
		});
	}


	/**
	 * 初始化springview组件
	 */
	private void  initSpringView(){
		springView = (SpringView) rootView.findViewById(R.id.springview);
		springView.setType(SpringView.Type.FOLLOW);
		springView.setListener(new SpringView.OnFreshListener() {
			@Override
			public void onRefresh() {
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						initData();
					}
				}, 1000);

			}

			@Override
			public void onLoadmore() {
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						springView.onFinishFreshAndLoad();
					}
				}, 1000);
			}
		});
		springView.setHeader(new DefaultHeader(getActivity()));
		//springView.setFooter(new DefaultFooter(getActivity()));
	}

	public static DeviceList newInstance() {
		DeviceList fragment = new DeviceList();
		return fragment;
	}

	public View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
				case R.id.iv_add:
					((SonActivity) getActivity()).addFragment(AddCar_A.newInstance());
					break;
			}
		}
	};


	@Override
	public boolean onBackPressed() {
		return false;
	}
	
}
